<?php
include_once('common/header.php');
if($AppUI->id <=0) {
	Vfriend_Function::redirect(HOST_VFRIEND);
}
$strRefer		= isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
if(!Vfriend_Function::isValidLink($strRefer)) {
	Vfriend_Function::redirect(LINK_PLACE);
}

$intCategoryId		= $objRequest->getParam('pcategoryid', 0);
$intSubCategoryId	= $objRequest->getParam('psubcategoryid', 0);
$strName			= $objRequest->getParam('pname', '');
$strCountry			= $objRequest->getParam('pcountry', COUNTRY_DEFAULT);
$intProvince		= $objRequest->getParam('pprovince', 0);
$intDistrict		= $objRequest->getParam('pdistrict', 0);
$strAddress			= $objRequest->getParam('paddress', '');
$oFile				= $_FILES['pimage'];
$strContent			= $objRequest->getParam('pcontent','');
$strMap				= $objRequest->getParam('pmap', '');
$strImage			= '';
$intError			= 0;

//if(!$intCategoryId) $intError = 9;
if(!$intSubCategoryId) $intError = 10;
elseif($strName == '') $intError = 1;
elseif($strName != '' && strlen($strName) < 5) $intError = 2;
elseif(!$intProvince) $intError = 4;
elseif(!$intDistrict) $intError = 12;
elseif($strAddress == '') $intError = 13;
elseif(!isset($oFile['name'])) $intError = 11;
//elseif(strip_tags($strContent) == '') $intError = 5;
//elseif(strip_tags($strContent) != '' && strlen(strip_tags($strContent))<20) $intError = 6;
//elseif($strMap = '') $intError = 14;

if($intError == 0 && isset($oFile['name']) && $oFile['name'] != '') {
	$arrImage = Vfriend_Upload::uploadPlaceImg($oFile, $strImage);	
	if($arrImage['error'] > 0) {
		$intError = 3;
	}
}

if($intError == 0) {
	if(isset($_SESSION['sPlacesError'])) unset($_SESSION['sPlacesError']);
	if(isset($_SESSION['sPlacesParam'])) unset($_SESSION['sPlacesParam']);
	$objPlaces	= new Place();
	$intError	= $objPlaces->add($AppUI->id, $strName, $intCategoryId, $intSubCategoryId, $strCountry, $intProvince, $intDistrict, $strAddress, $strContent, $strMap, $strImage, $intPlaceId);
	Vfriend_Function::setMsg($arrMessage[1]);
	$strLink = Vfriend_Function::linkPlaces($intPlaceId, $strName);
	Vfriend_Function::redirect($strLink);
}
if($intError > 0) {
	$_SESSION['sPlacesError']	= $arrError[$intError];
	$_SESSION['sPlacesParam']	= $objRequest->getParams();
	Vfriend_Function::setMsg($arrError[$intError]);
	Vfriend_Function::redirect(LINK_PLACE_ADD);
}
?>
